package com.cheerscpl.protocol;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by chenpuliang on 2017/6/4.
 */
public class Channels implements Serializable{

    private static final long serialVersionUID = 1609628302933043401L;

    private static Map<String, SocketChannel> channelMap = new ConcurrentHashMap<>();

    public synchronized static void add(String devId, SocketChannel sh){
        channelMap.put(devId, sh);
    }

    public synchronized static Channel getChannel(String devId){
        return channelMap.get(devId);
    }

    public synchronized static void remove(SocketChannel sh){
        for (Map.Entry entry : channelMap.entrySet()){
            if(entry.getValue() == sh){
                channelMap.remove(entry.getKey());
            }
        }
    }

    public synchronized static Set<String> getMapKeys(){
        return channelMap.keySet();
    }

    public synchronized static Set<SocketChannel> getChannels(){
        return (Set<SocketChannel>) channelMap.values();
    }

    public synchronized static void remove(String clientId){
        channelMap.remove(clientId);
    }
}
